package com.sgb.kjwl.viewmodel.self;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.link.entity.InvoiceManagementEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.self.InvoiceAddActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 */
public class InvoiceAddModel extends BaseViewModel {

    public ObservableField<InvoiceManagementEntity> invoiceManagementEntity = new ObservableField<>(new InvoiceManagementEntity());
    private InvoiceAddActivity activity;
    private SelfRepertory repertory;

    public InvoiceAddModel(InvoiceAddActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    /**
     * 获取当前id的地址信息
     */
    public void billDetail(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().billDetail(id).enqueue(new Callback<BaseEntity<InvoiceManagementEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<InvoiceManagementEntity>> call, Response<BaseEntity<InvoiceManagementEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && response.body().getState().equals("ok")) {
                    invoiceManagementEntity.set(response.body().getData());
                    activity.setData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<InvoiceManagementEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * 修改新增发票
     * LeaseEvent event = new LeaseEvent();
     * event.setMessageTag(EventConstant.EVENT_GOODS_FACTORY_LIJICANYU_KAIPIAOXINXI);
     * event.setMessageData(invoiceManagementEntity);
     * EventBus.getDefault().postSticky(event);
     * finish();
     */
    public void addBill() {
        DialogHelper.showProgressDialog(activity, null, "数据提交中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().addBill(invoiceManagementEntity.get()).enqueue(new Callback<BaseEntity<InvoiceManagementEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<InvoiceManagementEntity>> call, Response<BaseEntity<InvoiceManagementEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    MToast.showToast(activity, response.body().getMsg());
                    if (response.body().getState().equals("ok")) {
                        LeaseEvent event = new LeaseEvent();
                        event.setMessageTag(EventConstant.EVENT_GOODS_FACTORY_LIJICANYU_KAIPIAOXINXI);
                        event.setMessageData(invoiceManagementEntity);
                        EventBus.getDefault().postSticky(event);
                        activity.finish();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<InvoiceManagementEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void deleteBill(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().deleteBill(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    MToast.showToast(activity, response.body().getData().toString());
                    activity.finish();
                }
                assert response.body() != null;
                MToast.showToast(activity, response.body().getMsg());

            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
